package com.galen.manual.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.galen.manual.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户Mapper接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据手机号查询用户
     * @param phone 手机号
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE phone = #{phone} AND deleted = 0")
    User selectByPhone(String phone);

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE username = #{username} AND deleted = 0")
    User selectByUsername(String username);

    /**
     * 查询用户及其管理员信息
     * @param userId 用户ID
     * @return 用户信息（包含管理员信息）
     */
    @Select("SELECT u.*, " +
            "CASE WHEN a.id IS NOT NULL THEN true ELSE false END as is_admin, " +
            "a.role as admin_role " +
            "FROM users u " +
            "LEFT JOIN admins a ON u.id = a.user_id AND a.deleted = 0 " +
            "WHERE u.id = #{userId} AND u.deleted = 0")
    User selectUserWithAdminInfo(Long userId);

    /**
     * 根据手机号查询用户及其管理员信息
     * @param phone 手机号
     * @return 用户信息（包含管理员信息）
     */
    @Select("SELECT u.*, " +
            "CASE WHEN a.id IS NOT NULL THEN true ELSE false END as is_admin, " +
            "a.role as admin_role " +
            "FROM users u " +
            "LEFT JOIN admins a ON u.id = a.user_id AND a.deleted = 0 " +
            "WHERE u.phone = #{phone} AND u.deleted = 0")
    User selectUserWithAdminInfoByPhone(String phone);

    /**
     * 根据openid查询用户
     * @param openid 微信openid
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE openid = #{openid} AND deleted = 0")
    User selectByOpenid(String openid);

    /**
     * 查询用户列表（管理员专用）
     * @param excludeAdmins 是否排除管理员
     * @return 用户列表（包含管理员信息）
     */
    @Select("SELECT u.*, " +
            "CASE WHEN a.id IS NOT NULL THEN true ELSE false END as is_admin, " +
            "a.role as admin_role, " +
            "al.last_login_time, " +
            "al.last_login_ip " +
            "FROM users u " +
            "LEFT JOIN admins a ON u.id = a.user_id AND a.deleted = 0 " +
            "LEFT JOIN admin_login_logs al ON a.id = al.admin_id " +
            "WHERE u.deleted = 0 " +
            "AND (#{excludeAdmins} = false OR a.id IS NULL) " +
            "ORDER BY u.create_time DESC")
    java.util.List<User> selectUserListForManage(@Param("excludeAdmins") Boolean excludeAdmins);

    /**
     * 根据状态查询用户数量
     * @param status 用户状态
     * @param excludeAdmins 是否排除管理员
     * @return 用户数量
     */
    @Select("SELECT COUNT(*) FROM users u " +
            "LEFT JOIN admins a ON u.id = a.user_id AND a.deleted = 0 " +
            "WHERE u.deleted = 0 " +
            "AND u.status = #{status} " +
            "AND (#{excludeAdmins} = false OR a.id IS NULL)")
    Integer countUsersByStatus(@Param("status") Integer status, @Param("excludeAdmins") Boolean excludeAdmins);

    /**
     * 更新用户状态
     * @param userId 用户ID
     * @param status 新状态
     * @return 更新结果
     */
    @Update("UPDATE users SET status = #{status}, update_time = NOW() WHERE id = #{userId} AND deleted = 0")
    int updateUserStatus(@Param("userId") Long userId, @Param("status") Integer status);
}